<?php
namespace app\shop\controller;
use app\ApiRest;
use app\member\model\Member;
use app\Rest;
use think\App;
use think\Request;
use app\shop\model\IndexShopCar as CarModel;
use app\shop\model\IndexGoods;
use app\shop\model\IndexShopSpePrice;


class IndexShopCar extends ApiRest
{

    protected $model;
    protected $goods_model;
    protected $spe_price_model;
    public function __construct(App $app) {
        parent::__construct($app);
        $this->model       = new CarModel();
        $this->goods_model = new IndexGoods();
        $this->spe_price_model = new IndexShopSpePrice();
    }
    /**
     * 获取购物车列表
     */
    public function carList(){

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        $dis[] = ['a.status' ,'=',1];

        $dis[] = ['a.user_id','=',$this->getUserId()];

        $data  = $this->model->carList($dis);
        //会员模型
        $member_model  = new Member();

        if(!empty($data['list'])){

            foreach ($data['list'] as &$v){
                //会员商品信息
                $v['member_info'] = $member_model->goodsInfoMember($v['goods_id'],$this->getUserId(),$this->_uniacid);

            }

        }

        return $this->success($data);
    }


    /**
     * 加入购物车
     */

    public function carAdd(){
        $input = $this->_input;
        $car   = $this->model->carFind([
            'goods_id'     => $input['goods_id'],
            'spe_price_id' => $input['spe_price_id'],
            'user_id'      => $this->getUserId(),
            'status'       => 1
        ]);
        $goods = $this->goods_model->goodsInfo(['id'=>$input['goods_id']]);
        if(empty($goods)){
            return $this->error('没有该商品');
        }
        $spe = $this->spe_price_model->SpePriceFind(['id'=>$input['spe_price_id']]);
        if(empty($spe)){
            return $this->error('没有该规格');
        }
        $number = !empty($car)?$car['number']+$input['number']:$input['number'];
        if($number>$spe['stock']){
            return $this->error('库存不足');
        }
        if(!empty($car)&&$number<=0){ //如果数量小于0就删除
            $this->model->carDel([$car['id']]);
        }else{
            $data   = array(
                'goods_id'     => $input['goods_id'],
                'user_id'      => $this->getUserId(),
                'name'         => $goods['name'],
                'cover'        => $goods['cover'],
                'spe_price_id' => $input['spe_price_id'],
                'content'      => $spe['spe_name_text'],
                'number'       => $number,
                'uniacid'      => $this->_uniacid,
                'price'        => $spe['price']*$number,
                'freight'      => $goods['freight'],
                'status'       => $input['number']==0?-1:1
            );
            if(empty($car)){
                $this->model->carAdd($data);
            }else{
                $this->model->carUpdate(['id'=>$car['id']],$data);
            }
        }
        $data = $this->model->carCount(['uniacid'=>$this->_uniacid,'user_id'=>$this->getUserId(),'status'=>1]);
        return $this->success($data);
    }

    /**
     * 批量删除
     */

    public function carDel(){
        $input = $this->_input;
        $res   = $this->model->carDel($input['id']);
        return $this->success($res);
    }



























}
